package com.yunshipei.parser;

import android.text.TextUtils;
import android.util.Log;

import com.yunshipei.model.DateTime;
import com.yunshipei.model.SpeechRecognition;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

/**
 * Created by cike on 15-11-19.
 */
public class SpeechXMLParser {
    private DocumentBuilderFactory documentBuilderFactory = null;
    private DocumentBuilder documentBuilder = null;

    public SpeechXMLParser() {
        try {
            documentBuilderFactory = DocumentBuilderFactory.newInstance();
            documentBuilder = documentBuilderFactory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
    }

    /**
     * 解析语音识别结果(xml)
     * @param str
     * @return
     */
    public SpeechRecognition parser(String str) {
        SpeechRecognition speechRecognition = new SpeechRecognition();
        try {
            InputStream input = new ByteArrayInputStream(str.getBytes());
            Document document = documentBuilder.parse(input);
            Element root = document.getDocumentElement();
            NodeList nodes = root.getChildNodes();
            for (int i = 0; i < nodes.getLength(); i++) {
                String childEleName = nodes.item(i).getNodeName();
                //status
                if (!TextUtils.isEmpty(childEleName)) {
                    if (childEleName.equals("status")) {
                        speechRecognition.setStatus(nodes.item(i).getFirstChild().getNodeValue());
                    } else if (childEleName.equals("error_code")) {
                        speechRecognition.setErrorCode(nodes.item(i).getFirstChild().getNodeValue());
                    } else if (childEleName.equals("desc")) {
                        speechRecognition.setDesc(nodes.item(i).getFirstChild().getNodeValue());
                    } else if (childEleName.equals("result")) {
                        NodeList resultChild = nodes.item(i).getChildNodes();
                        for (int j = 0; j < resultChild.getLength(); j++) {
                            String resultChildName = resultChild.item(j).getNodeName();
                            Log.d("resultChildName", resultChildName);
                            if (!TextUtils.isEmpty(resultChildName)) {
                                if (resultChildName.equals("focus")) {
                                    speechRecognition.setFocus(resultChild.item(j).getFirstChild().getNodeValue());
                                } else if (resultChildName.equals("action")) {
                                    NodeList actionChild = resultChild.item(j).getChildNodes();
                                    for (int k = 0; k< actionChild.getLength(); k++) {
                                        String actionChildName = actionChild.item(k).getNodeName();
                                        if (!TextUtils.isEmpty(actionChildName)) {
                                            if (actionChildName.equals("operation")) {
                                                speechRecognition.setOperation(actionChild.item(k).getFirstChild().getNodeValue());
                                            } else if (actionChildName.equals("tip")) {
                                                speechRecognition.setTip(actionChild.item(k).getFirstChild().getNodeValue());
                                            } else if (actionChildName.equals("speech")) {
                                                speechRecognition.setSpeech(actionChild.item(k).getFirstChild().getNodeValue());
                                            }
                                        }
                                    }
                                } else if (resultChildName.equals("content")) {
                                    speechRecognition.setContent(resultChild.item(j).getFirstChild().getNodeValue());
                                } else if (resultChildName.equals("object")) {
                                    NodeList objChild = resultChild.item(j).getChildNodes();
                                    for (int l = 0; l < objChild.getLength(); l++) {
                                        String objName = objChild.item(l).getNodeName();
                                        if (!TextUtils.isEmpty(objName)) {
                                            if (objName.equals("name")) {
                                                speechRecognition.setName(objChild.item(l).getFirstChild().getNodeValue());
                                            } else if (objName.equals("datetime")) {
                                                DateTime dt = new DateTime();
                                                Element objEle = (Element) objChild.item(l);
                                                dt.setDate(objEle.getAttribute("date"));
                                                dt.setDateOrig(objEle.getAttribute("date_orig"));
                                                dt.setTime(objEle.getAttribute("time"));
                                                dt.setTimeOrig(objEle.getAttribute("time_orig"));
                                                speechRecognition.setDateTime(dt);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return speechRecognition;
    }
}
